﻿using System;
using System.IO;
using System.Net.Sockets;
namespace TcpConnector
{
    public struct Msg
    {
        public int Type;
        public int Size;
        public byte[] Content;
    }
    public class Connector
    {

        const int HEAD_SIZE = 4;
        private TcpClient client;
        NetworkStream stream;

        public bool Connect(string ip, int port)
        {
            try
            {
                client = new TcpClient(ip, port);
                stream = client.GetStream();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public void Disconnect()
        {
            stream.Close();
            client.Close();
        }
        private int readType()
        {
            byte[] headData = new byte[HEAD_SIZE];
            stream.Read(headData, 0, headData.Length);

            int msgType = BitConverter.ToInt32(headData, 0);
            return msgType;
        }
        private int readSize()
        {
            byte[] headData = new byte[HEAD_SIZE];
            stream.Read(headData, 0, headData.Length);

            int msgSize = BitConverter.ToInt32(headData, 0);
            return msgSize;
        }
        private byte[] readContent(int leghth)
        {
            byte[] content = new byte[leghth];
            stream.Read(content, 0, content.Length);
            return content;
        }
        public Msg Read()
        {
            Msg msg = new Msg();
            msg.Type = readType();
            msg.Size = readSize();

            if (msg.Size > 0)
            {
                msg.Content = readContent(msg.Size);
            }
            return msg;
        }
        public void Write(int msgType, byte[] msgContent)
        {

            byte[] msgTypeByte = BitConverter.GetBytes(msgType);

            int msgSize = HEAD_SIZE + HEAD_SIZE + msgContent.Length;
            byte[] msgSizeByte = BitConverter.GetBytes(msgSize);

            int totalSize = HEAD_SIZE + HEAD_SIZE + msgSize;
            byte[] msgByte = new byte[totalSize];

            int index = 0;
            int i = 0;
            for (i = 0; i < HEAD_SIZE; i++)
            { // put msg type
                if (msgTypeByte.Length > i)
                {
                    msgByte[index] = msgTypeByte[i];
                }
                index++;
            }
            for (i = 0; i < HEAD_SIZE; i++)
            { // put msg size
                if (msgTypeByte.Length > i)
                {
                    msgByte[index + i] = msgSizeByte[i];
                }
                index++;
            }
            for (i = 0; i < msgSize; i++)
            { // put msg content
                if (msgTypeByte.Length > i)
                {
                    msgByte[index + i] = msgContent[i];
                }
                index++;
            }
            stream.Write(msgByte, 0, msgByte.Length);
            stream.Flush();
        }
    }
}